<template>
    <div class="p-6 space-y-6">
        <!-- 页面标题 -->
        <div class="flex items-center justify-between">
            <div>
                <h2 class="text-2xl font-bold text-gray-800 dark:text-gray-200">元数据字段</h2>
                <p class="text-gray-600 dark:text-gray-400 mt-1">管理数据模型字段定义</p>
            </div>
            <button class="btn-primary flex items-center gap-2">
                <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                          d="M12 6v6m0 0v6m0-6h6m-6 0H6" />
                </svg>
                新增字段
            </button>
        </div>

        <!-- 字段统计 -->
        <div class="grid grid-cols-1 md:grid-cols-4 gap-6">
            <div class="card text-center">
                <div class="text-3xl font-bold text-blue-600 dark:text-blue-400 mb-2">{{ totalFields }}</div>
                <div class="text-gray-600 dark:text-gray-400">总字段数</div>
            </div>
            <div class="card text-center">
                <div class="text-3xl font-bold text-green-600 dark:text-green-400 mb-2">{{ activeFields }}</div>
                <div class="text-gray-600 dark:text-gray-400">启用字段</div>
            </div>
            <div class="card text-center">
                <div class="text-3xl font-bold text-purple-600 dark:text-purple-400 mb-2">{{ fieldTypes }}</div>
                <div class="text-gray-600 dark:text-gray-400">字段类型</div>
            </div>
            <div class="card text-center">
                <div class="text-3xl font-bold text-orange-600 dark:text-orange-400 mb-2">{{ requiredFields }}</div>
                <div class="text-gray-600 dark:text-gray-400">必填字段</div>
            </div>
        </div>

        <!-- 搜索和筛选 -->
        <div class="card">
            <div class="flex flex-col md:flex-row gap-4">
                <div class="flex-1">
                    <div class="relative">
                        <svg class="absolute left-3 top-1/2 transform -translate-y-1/2 w-5 h-5 text-gray-400"
                             fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                  d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
                        </svg>
                        <input type="text" placeholder="搜索字段名称..." class="input pl-10" />
                    </div>
                </div>
                <div class="flex gap-2">
                    <select class="input">
                        <option>所有模型</option>
                        <option>用户模型</option>
                        <option>角色模型</option>
                        <option>部门模型</option>
                    </select>
                    <select class="input">
                        <option>所有类型</option>
                        <option>int</option>
                        <option>varchar</option>
                        <option>text</option>
                        <option>enum</option>
                    </select>
                    <button class="btn-secondary">筛选</button>
                </div>
            </div>
        </div>

        <!-- 字段列表 -->
        <div class="card">
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead>
                        <tr class="border-b border-gray-200 dark:border-gray-700">
                            <th class="text-left p-4 font-semibold text-gray-800 dark:text-gray-200">字段信息</th>
                            <th class="text-left p-4 font-semibold text-gray-800 dark:text-gray-200">所属模型</th>
                            <th class="text-left p-4 font-semibold text-gray-800 dark:text-gray-200">字段类型</th>
                            <th class="text-left p-4 font-semibold text-gray-800 dark:text-gray-200">约束</th>
                            <th class="text-left p-4 font-semibold text-gray-800 dark:text-gray-200">操作</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200 dark:divide-gray-700">
                        <tr v-for="field in fields" :key="field.id"
                            class="hover:bg-gray-50 dark:hover:bg-gray-800/50 transition-colors">
                            <td class="p-4">
                                <div>
                                    <div class="font-medium text-gray-900 dark:text-gray-100">{{ field.name }}</div>
                                    <div class="text-sm text-gray-500 dark:text-gray-400">{{ field.description }}</div>
                                </div>
                            </td>
                            <td class="p-4">
                                <div class="flex items-center gap-2">
                                    <span :class="getModelBadgeClass(field.modelCategory)">{{ field.modelName }}</span>
                                </div>
                            </td>
                            <td class="p-4">
                                <span :class="getFieldTypeClass(field.type)">{{ field.type }}</span>
                                <div v-if="field.length" class="text-xs text-gray-500 dark:text-gray-400 mt-1">
                                    长度: {{ field.length }}
                                </div>
                            </td>
                            <td class="p-4">
                                <div class="flex flex-wrap gap-1">
                                    <span v-if="field.required"
                                          class="px-2 py-1 bg-red-100 text-red-800 dark:bg-red-900/20 dark:text-red-400 text-xs rounded-full">
                                        必填
                                    </span>
                                    <span v-if="field.unique"
                                          class="px-2 py-1 bg-blue-100 text-blue-800 dark:bg-blue-900/20 dark:text-blue-400 text-xs rounded-full">
                                        唯一
                                    </span>
                                    <span v-if="field.index"
                                          class="px-2 py-1 bg-green-100 text-green-800 dark:bg-green-900/20 dark:text-green-400 text-xs rounded-full">
                                        索引
                                    </span>
                                </div>
                            </td>
                            <td class="p-4">
                                <div class="flex items-center gap-2">
                                    <button
                                            class="p-2 text-blue-600 hover:bg-blue-50 dark:hover:bg-blue-900/20 rounded-lg transition-colors">
                                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                                  d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" />
                                        </svg>
                                    </button>
                                    <button
                                            class="p-2 text-red-600 hover:bg-red-50 dark:hover:bg-red-900/20 rounded-lg transition-colors">
                                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                                  d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
                                        </svg>
                                    </button>
                                </div>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>

            <!-- 分页 -->
            <div class="flex items-center justify-between mt-6">
                <div class="text-sm text-gray-500 dark:text-gray-400">
                    显示 1-10 条，共 {{ fields.length }} 条记录
                </div>
                <div class="flex items-center gap-2">
                    <button class="p-2 text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 disabled:opacity-50"
                            disabled>
                        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7" />
                        </svg>
                    </button>
                    <button class="px-3 py-2 bg-blue-600 text-white rounded-lg">1</button>
                    <button
                            class="px-3 py-2 text-gray-600 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700 rounded-lg">2</button>
                    <button
                            class="px-3 py-2 text-gray-600 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700 rounded-lg">3</button>
                    <button class="p-2 text-gray-400 hover:text-gray-600 dark:hover:text-gray-300">
                        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" />
                        </svg>
                    </button>
                </div>
            </div>
        </div>

        <!-- 字段详情弹窗 -->
        <div v-if="showFieldModal" class="fixed inset-0 z-50 flex items-center justify-center bg-black/50">
            <div
                 class="bg-white dark:bg-gray-800 rounded-xl shadow-2xl p-6 w-full max-w-2xl max-h-[80vh] overflow-y-auto">
                <div class="flex items-center justify-between mb-6">
                    <h3 class="text-xl font-bold text-gray-900 dark:text-gray-100">字段详情 - {{ selectedField?.name }}</h3>
                    <button @click="showFieldModal = false"
                            class="text-gray-400 hover:text-gray-600 dark:hover:text-gray-300">
                        <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                  d="M6 18L18 6M6 6l12 12" />
                        </svg>
                    </button>
                </div>

                <div class="space-y-4">
                    <div class="grid grid-cols-2 gap-4">
                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">字段名称</label>
                            <input type="text" :value="selectedField?.name" class="input" />
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">字段编码</label>
                            <input type="text" :value="selectedField?.code" class="input" />
                        </div>
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">所属模型</label>
                        <select class="input">
                            <option value="user">用户模型</option>
                            <option value="role">角色模型</option>
                            <option value="department">部门模型</option>
                            <option value="menu">菜单模型</option>
                        </select>
                    </div>

                    <div class="grid grid-cols-2 gap-4">
                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">字段类型</label>
                            <select class="input">
                                <option value="int">整数 (int)</option>
                                <option value="varchar">字符串 (varchar)</option>
                                <option value="text">文本 (text)</option>
                                <option value="enum">枚举 (enum)</option>
                                <option value="datetime">日期时间 (datetime)</option>
                                <option value="boolean">布尔值 (boolean)</option>
                            </select>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">字段长度</label>
                            <input type="number" :value="selectedField?.length" class="input" placeholder="可选" />
                        </div>
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">描述</label>
                        <textarea class="input" rows="3" :value="selectedField?.description"></textarea>
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">约束设置</label>
                        <div class="space-y-2">
                            <label class="flex items-center gap-2">
                                <input type="checkbox"
                                       class="rounded border-gray-300 text-blue-600 focus:ring-blue-500" />
                                <span class="text-sm text-gray-700 dark:text-gray-300">必填字段</span>
                            </label>
                            <label class="flex items-center gap-2">
                                <input type="checkbox"
                                       class="rounded border-gray-300 text-blue-600 focus:ring-blue-500" />
                                <span class="text-sm text-gray-700 dark:text-gray-300">唯一字段</span>
                            </label>
                            <label class="flex items-center gap-2">
                                <input type="checkbox"
                                       class="rounded border-gray-300 text-blue-600 focus:ring-blue-500" />
                                <span class="text-sm text-gray-700 dark:text-gray-300">创建索引</span>
                            </label>
                        </div>
                    </div>
                </div>

                <div
                     class="flex items-center justify-end gap-3 mt-6 pt-4 border-t border-gray-200 dark:border-gray-700">
                    <button @click="showFieldModal = false" class="btn-secondary">取消</button>
                    <button class="btn-primary">保存</button>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts" name="meta-field">
import { ref, computed } from 'vue'

defineOptions({ name: 'meta-field' })

const fields = ref([
    {
        id: 1,
        name: '用户ID',
        code: 'id',
        description: '用户唯一标识符',
        modelName: '用户模型',
        modelCategory: 'business',
        type: 'int',
        length: null,
        required: true,
        unique: true,
        index: true
    },
    {
        id: 2,
        name: '用户名',
        code: 'username',
        description: '用户登录用户名',
        modelName: '用户模型',
        modelCategory: 'business',
        type: 'varchar',
        length: 50,
        required: true,
        unique: true,
        index: true
    },
    {
        id: 3,
        name: '邮箱',
        code: 'email',
        description: '用户邮箱地址',
        modelName: '用户模型',
        modelCategory: 'business',
        type: 'varchar',
        length: 100,
        required: true,
        unique: true,
        index: false
    },
    {
        id: 4,
        name: '密码',
        code: 'password',
        description: '用户登录密码',
        modelName: '用户模型',
        modelCategory: 'business',
        type: 'varchar',
        length: 255,
        required: true,
        unique: false,
        index: false
    },
    {
        id: 5,
        name: '状态',
        code: 'status',
        description: '用户状态',
        modelName: '用户模型',
        modelCategory: 'business',
        type: 'enum',
        length: null,
        required: true,
        unique: false,
        index: true
    },
    {
        id: 6,
        name: '角色名称',
        code: 'name',
        description: '角色显示名称',
        modelName: '角色模型',
        modelCategory: 'system',
        type: 'varchar',
        length: 50,
        required: true,
        unique: false,
        index: false
    },
    {
        id: 7,
        name: '角色编码',
        code: 'code',
        description: '角色唯一编码',
        modelName: '角色模型',
        modelCategory: 'system',
        type: 'varchar',
        length: 30,
        required: true,
        unique: true,
        index: true
    },
    {
        id: 8,
        name: '部门名称',
        code: 'name',
        description: '部门显示名称',
        modelName: '部门模型',
        modelCategory: 'business',
        type: 'varchar',
        length: 100,
        required: true,
        unique: false,
        index: false
    },
    {
        id: 9,
        name: '上级部门',
        code: 'parent_id',
        description: '上级部门ID',
        modelName: '部门模型',
        modelCategory: 'business',
        type: 'int',
        length: null,
        required: false,
        unique: false,
        index: true
    },
    {
        id: 10,
        name: '菜单路径',
        code: 'path',
        description: '菜单访问路径',
        modelName: '菜单模型',
        modelCategory: 'system',
        type: 'varchar',
        length: 200,
        required: true,
        unique: false,
        index: false
    }
])

const showFieldModal = ref(false)
const selectedField = ref<any>(null)

const totalFields = computed(() => fields.value.length)
const activeFields = computed(() => fields.value.length) // 假设所有字段都是启用的
const fieldTypes = computed(() => new Set(fields.value.map(field => field.type)).size)
const requiredFields = computed(() => fields.value.filter(field => field.required).length)

function getModelBadgeClass(category: string) {
    const classes = {
        'business': 'px-2 py-1 bg-blue-100 text-blue-800 dark:bg-blue-900/20 dark:text-blue-400 text-xs rounded-full',
        'system': 'px-2 py-1 bg-green-100 text-green-800 dark:bg-green-900/20 dark:text-green-400 text-xs rounded-full',
        'config': 'px-2 py-1 bg-purple-100 text-purple-800 dark:bg-purple-900/20 dark:text-purple-400 text-xs rounded-full'
    }
    return classes[category as keyof typeof classes] || classes['business']
}

function getFieldTypeClass(type: string) {
    const classes = {
        'int': 'px-2 py-1 bg-blue-100 text-blue-800 dark:bg-blue-900/20 dark:text-blue-400 text-xs rounded-full',
        'varchar': 'px-2 py-1 bg-green-100 text-green-800 dark:bg-green-900/20 dark:text-green-400 text-xs rounded-full',
        'text': 'px-2 py-1 bg-purple-100 text-purple-800 dark:bg-purple-900/20 dark:text-purple-400 text-xs rounded-full',
        'enum': 'px-2 py-1 bg-orange-100 text-orange-800 dark:bg-orange-900/20 dark:text-orange-400 text-xs rounded-full',
        'datetime': 'px-2 py-1 bg-red-100 text-red-800 dark:bg-red-900/20 dark:text-red-400 text-xs rounded-full',
        'boolean': 'px-2 py-1 bg-gray-100 text-gray-800 dark:bg-gray-900/20 dark:text-gray-400 text-xs rounded-full'
    }
    return classes[type as keyof typeof classes] || classes['varchar']
}
</script>